package com.ysd.lis.service.sta.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.sta.StaModelDto;
import com.ysd.lis.dto.sta.StaModelParam;
import com.ysd.lis.entity.sta.StaModelModule;
import com.ysd.lis.entity.sta.StaModule;
import com.ysd.lis.mapper.sta.StaModelModuleMapper;
import com.ysd.lis.service.sta.StaModelModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * cold_chain.sys_Model 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-12-27
 */
@Service
public class StaModelModuleServiceImpl extends ServiceImpl<StaModelModuleMapper, StaModelModule> implements StaModelModuleService {
    @Autowired
    StaModelModuleMapper staModelModuleMapper;
    @Override
    public Result findStaModelModuleList(StaModelParam param) {
        MPJLambdaWrapper<StaModelModule> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(StaModelModule.class)
                .selectAs("ld", StaModule::getModCode,StaModelModule::getModCode)
                .selectAs("ld", StaModule::getModName,StaModelModule::getModName)
                .selectAs("ld", StaModule::getIcon,StaModelModule::getIcon)
                .leftJoin(StaModule.class,"ld",StaModule::getId,StaModelModule::getStaModId)
                .eq(StaModelModule::getModelId,param.getModelId())
                .eq(StaModelModule::getDelFlag,0)
                .orderByAsc(StaModelModule::getSeq);
        List<StaModelModule> itemList = staModelModuleMapper.selectList(queryWrapper);

        return Result.succ(1, "查询成功", itemList);
    }

    @Override
    public Result batchDeleteStaModelModuleList(StaModelDto dto) {
        List<StaModelModule> staModelModuleList = dto.getStaModelModuleList();
        List<String> ids = staModelModuleList.stream().map(StaModelModule::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }

    @Override
    public Result batchAddStaModelModuleList(StaModelDto dto) {
        List<StaModelModule> staModelModuleList = dto.getStaModelModuleList();
        boolean b = this.saveBatch(staModelModuleList);
        if(b){
            return Result.succ(1, "添加成功", null);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }
}
